<?php

namespace App\Http\Controllers;

use App\Http\Requests\PageRequest;
use App\Model\Asset;
use App\Model\Page;
use Chumper\Zipper\Facades\Zipper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Psy\Util\Json;

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('page.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param PageRequest|Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(PageRequest $request)
    {

            $file = $request->file('file');
            $ext = $file->extension();
            $md5 = md5_file($file);
            $mime = $file->getMimeType();
            if ($ext !== 'zip') {
                return response(['message' => '上传格式不正确'], 403);
            }

            if(Asset::find($md5)){
                return response(['message' => '该文件已存在'], 403);
            }

            $asset = new Asset([
                'id' => $md5,
                'path' => 'zip',
                'ext' => $ext,
                'mime' => $mime,
            ]);
            $page = new Page([
                'name' => $request->get('name'),
                'entry' => $request->get('entry'),
                'tag' => $request->get('tag', []),
                'version' => $request->get('version'),
                'user_id' => $request->user()->id
            ]);

            DB::beginTransaction();
            if ($asset->save() && $asset->page()->save($page)) {
                $result = $file->storePubliclyAs('public/zip', $md5 . '.zip');
                Zipper::make(ltrim(Storage::url($result),'/'))->extractTo('html/'.$md5);
                DB::commit();
            } else {
                DB::rollBack();
            }
//            dd($asset);
            return response([
                'message'=>'提交成功'
            ]);

        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $asset_id=Page::find($id)->asset_id;
        $entry=Page::find($id)->entry;
        $path=url('html/'.$asset_id.'/'.$entry);
        return redirect($path);
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $page=Page::find($id);
        return view('page.edit',['page'=>$page]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(PageRequest $request, $id)
    {
        $page=Page::find($id);

        $page->update(
            [
                'id'=>$id,
                'name'=>$request->get('name'),
                'entry'=>$request->get('entry'),
                'tag'=>$request->get('tag')
            ]
        );
        return response(['message'=>'修改成功',$request->all()]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
